package com.example.robertluozizhao.java.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Lzz on 7/21/2019.
 */

public class ThreadPoolsExecutorTestDemo {

    public static void main(String args[]){


//        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

        //01 执行结果一样 等待核心线程完，复用核心线程再执行第四个线程任务
       /* ThreadPoolExecutor executor = new ThreadPoolExecutor(3,10,
                60L, TimeUnit.SECONDS,new *//*LinkedBlockingQueue*//*ArrayBlockingQueue<Runnable>(10));
*/
       //02 自定义
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,4,
                5L, TimeUnit.MILLISECONDS,new SynchronousQueue<Runnable>());

        TestRunnable testRunnable = new TestRunnable();
/*        fixedThreadPool.execute(testRunnable);
        fixedThreadPool.execute(testRunnable);
        fixedThreadPool.execute(testRunnable);
        fixedThreadPool.execute(testRunnable);//*/

        executor.execute(testRunnable);
        executor.execute(testRunnable);
        executor.execute(testRunnable);
        executor.execute(testRunnable);

    }

    public static class TestRunnable implements Runnable{
        @Override
        public void run() {
            try {
                System.out.println("线程id:"+Thread.currentThread().getId()+",线程："+Thread.currentThread().getName());
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
